package com.future.hedongjun.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.future.hedongjun.R;
import com.future.hedongjun.home.bean.AudioItem;
import com.future.hedongjun.home.bean.ClassHourBean;

import java.util.List;

/**
 * Created by Godot on 2017/6/1.
 */

public class AudioListAdapter extends BaseAdapter {
    private Context mContext;
    private int mResource;
    private LayoutInflater mInflater;
    private List<ClassHourBean> mList;
    private SparseBooleanArray mBooleanArray;

    private static class ViewHolder {
        TextView title;
        TextView status;
        TextView noBuy;
    }

    public AudioListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<ClassHourBean> objects, SparseBooleanArray selectData) {
        mContext = context;
        mResource = resource;
        mInflater = LayoutInflater.from(mContext);
        mList = objects;
        mBooleanArray = selectData;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public ClassHourBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(mResource, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.tv_classTitle);
            viewHolder.status = (TextView) convertView.findViewById(R.id.tv_classPause);
            viewHolder.noBuy = (TextView) convertView.findViewById(R.id.tv_classNoBuy);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ClassHourBean audioItem = getItem(position);
        viewHolder.title.setText(audioItem.title);

        if (audioItem.buy_status.equals("3")) {
            viewHolder.noBuy.setVisibility(View.GONE);
        }

        if (mBooleanArray.get(position)) {
            viewHolder.title.setTextColor(Color.parseColor("#CAD39B"));
            viewHolder.status.setBackgroundResource(R.drawable.yinping);
        } else {
            viewHolder.title.setTextColor(Color.parseColor("#ff333333"));
            viewHolder.status.setBackgroundResource(R.drawable.kecheng_bofang_hui);
        }
        return convertView;
    }
}
